/* 
 * File:   RapidHttpServlet.h
 * Author: dell
 *
 * Created on 2012年4月2日, 下午6:20
 */

#ifndef RAPIDHTTPSERVLET_H
#define	RAPIDHTTPSERVLET_H

#include <sys/stat.h>
#include <sys/file.h>
#include <string>
#include "common.h"
#include <evhttp/resources.h>
#include <evhttp/HttpServlet.h>
#include "config/ServerConfig.h"
#include "config/ContextConfig.h"
#include "FileCached.h"

namespace Rapid{
    class RapidHttpServlet : public HttpServlet{
    public:
        RapidHttpServlet(ServerConfig* serverConfig, ContextConfig* contextConfig, const char* subpath);
        virtual ~RapidHttpServlet();

        virtual void service(Request& req, Response& resp);
        
    protected:    
        typedef struct FILE{
            std::string physicalFilename; //文件全名，包括全物理路径
            std::string filename;
            FD fd;
            struct stat stat;

            FILE():fd(-1){}
        }FILE;    

    protected:
        /**
        * 当请求路径中没指定路径的时候， 打开可用的欢迎页面 
        */
        virtual void openWelcomeFile();

        /**
        * 打开请求路径中指定的文件
        */
        virtual void openRequestFile();    

        /**
        * 打开文件, 返回文件描述符, 下一步考虑从shm分区中打开文件
        */
        virtual void openFile(const char* filename);

        /**
        * 返回ContentTypeConfig信息
        */
        virtual ContentTypeConfig* getContentTypeConfig(const char* filetype);


    private:

        const char* getExpandName(std::string* filename);

    protected:
        FileCached fileCached;
        ServerConfig* serverConfig;
        ContextConfig* contextConfig;
        const char* subpath;
        FILE file;

    };
}
#endif	/* RAPIDHTTPSERVLET_H */

